package com.chis.coolplat;

import com.chis.jm.bean.ll.TdData;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年12月30日
 */
public class Baowen {

    private static String getData(String content,int pos1,int pos2,int pos3) {
        String tempStr = content.substring(pos2, pos3)+content.substring(pos1, pos2);
        Integer integer = Integer.valueOf(tempStr, 16);
        DecimalFormat df=new DecimalFormat("0.00");
        Float v = integer / 100F;
        if(v > 100){
            v = (Integer.valueOf(tempStr, 16) - 65536) / 100F;
        }
        return df.format(v);
    }

    public static void main(String[] args) {
        String content2 ="AA751B083630393130343034FC73F55D0264F18C18FD00FC64FB1BE0C7";
        Integer snByteLength = Integer.valueOf(content2.substring(6, 8), 16);
        String substring222 = content2.substring(8, 8+snByteLength*2);//sn号10byte 16进制转10长度
        String sn =ParseReportUtils.hexStringToString(substring222);//sn length 为10
        String content = content2.substring(4, 6) + content2.substring(8+snByteLength*2,content2.length()-4);

        String orignal = content2;
        TdData data = new TdData();
        boolean bool = false;
        //长度
        Integer cd = Integer.valueOf(content.substring(0, 2), 16);
        //sn号
        //时间
        Long timelong = Long.valueOf(ParseReportUtils.LowTransferHigh(content.substring(2, 10)), 16);
//        if(timelong == null || String.valueOf(timelong).length() != 10){
//            throw new Exception("数据上传原始值：" + orignal +"，上传采集时间必须为10位！");
//        }
        Date time = new Date(timelong * 1000L);
        //通道数
        Integer tds = Integer.valueOf(content.substring(10, 12), 16);

        //标识
        String flag=null;
        int length=0;
        if(tds==1){
            //温度1
            String format = getData(content,12,14,16);
            data.setData1(Float.valueOf(format));
            if(content.length()>16){
                flag=content.substring(16, 18);
                length=18;
            }

        }else if(tds==2){
            //温度1
            String format = getData(content,12,14,16);
            data.setData1(Float.valueOf(format));
            //控制传感器温度2
            Float data2 = Float.valueOf(getData(content, 16, 18, 20));
            data.setData2(data2);
            if(content.length()>20){
                flag=content.substring(20, 22);
                length=22;
            }

        }else if(tds==3){
            //温度1
            String format = getData(content,12,14,16);
            data.setData1(Float.valueOf(format));
            //控制传感器温度2
            Float data2 = Float.valueOf(getData(content, 16, 18, 20));
            data.setData2(data2);
            //湿度1
            Float data3 = Float.valueOf(getData(content, 20, 22, 24));
            data.setData3(data3);
            if(content.length()>24){
                flag=content.substring(24, 26);
                length=26;
            }

        }else if(tds==4){
            //温度1
            String format = getData(content,12,14,16);
            data.setData1(Float.valueOf(format));
            //控制传感器温度2
            Float data2 = Float.valueOf(getData(content, 16, 18, 20));
            data.setData2(data2);
            //湿度1
            Float data3 = Float.valueOf(getData(content, 20, 22, 24));
            data.setData3(data3);
            //湿度2
            Float data4 = Float.valueOf(getData(content, 24, 26, 28));
            data.setData4(data4);
            if(content.length()>28){
                flag=content.substring(28, 30);
                length=30;
            }

        }else if(tds==0){
            //温度1
            if(content.length() > 12){
                flag=content.substring(12, 14);
                length=14;
            }else{
                length=12;
            }
        }
        String substring = "";
        if(content.length()>length){
            substring = content.substring(length, length+2);
        }
        if("FE".equals(flag)){
            String lng = getGPSData(content,length,length+2,length+4,length+6,length+8);
            length = length + 8;
            data.setLng(Double.valueOf(lng));
            String lat = getGPSData(content,length,length+2,length+4,length+6,length+8);
            length = length + 8;
            data.setLat(Double.valueOf(lat));
            //在判断后一个标志位
            if(content.length()>length+2){
                String flag1 = content.substring(length, length+2);
                String substring1 = content.substring(length+2, length+4);
                if("FD".equals(flag1)){
                    bool = true;
                    //运行状态标识
                    String s = ParseReportUtils.hexString2binaryString(substring1);
                    Integer dystate = Integer.parseInt(s.substring(7, 8));
                    if (dystate == 1) {//电源故障
                        data.setState(2);
                    } else {
                        data.setState(1);
                    }
                    //在判断后一个标志位
                    if(content.length()>length+4){
                        flag1=content.substring(length+4, length+6);
                        substring1 = content.substring(length+6, length+8);
                        if("FC".equals(flag1)){
                            //备电电量标识
                            Integer dc = Integer.valueOf(substring1, 16);
                            data.setDy(dc.toString());
                            if(content.length() > length+8){
                                String flag2=content.substring(length+8, length+10);
                                String substring2=content.substring(length+10, length+12);
                                if("FB".equals(flag2)){
                                    //信号
                                    Integer xh = Integer.valueOf(substring2, 16);
                                    data.setXh(xh.toString());
                                }
                            }
                        }else if("FB".equals(flag1)){
                            //信号
                            Integer xh = Integer.valueOf(substring1, 16);
                            data.setXh(xh.toString());
                        }
                    }
                }
            }
        }
        else if("FD".equals(flag)){
            bool = true;
            //运行状态标识
            String s = ParseReportUtils.hexString2binaryString(substring);
            Integer dystate = Integer.parseInt(s.substring(7, 8));
            if (dystate == 1) {//电源故障
                data.setState(2);
            } else {
                data.setState(1);
            }

            //在判断后一个标志位
            if(content.length()>length+2){
                String flag1=content.substring(length+2, length+4);
                String substring1 = content.substring(length+4, length+6);
                if("FC".equals(flag1)){
                    //备电电量标识
                    Integer dc = Integer.valueOf(substring1, 16);
                    data.setDy(dc.toString());
                    if(content.length()>length+6){
                        String flag2=content.substring(length+6, length+8);
                        String substring2=content.substring(length+8, length+10);
                        if("FB".equals(flag2)){
                            //信号
                            Integer xh = Integer.valueOf(substring2, 16);
                            data.setXh(xh.toString());
                        }
                    }

                }else if("FB".equals(flag1)){
                    //信号
                    Integer xh = Integer.valueOf(substring1, 16);
                    data.setXh(xh.toString());
                }
            }

        }else if("FC".equals(flag)){
            //备电电量标识
            Integer dc = Integer.valueOf(substring, 16);
            data.setDy(dc.toString());
            if(content.length()>length+2){
                String flag1=content.substring(length+2, length+4);
                String substring1 = content.substring(length+4, length+6);
                if("FB".equals(flag1)){
                    //信号
                    Integer xh = Integer.valueOf(substring1, 16);
                    data.setXh(xh.toString());
                }
            }
        }else if("FB".equals(flag)){
            //信号
            Integer xh = Integer.valueOf(substring, 16);
            data.setXh(xh.toString());
        }
        if(!bool){
            System.out.println("数据上传原始值：" + orignal +"，FD标识未接收到，报文无效！");
        }else{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            String print = " sn:" + sn + " 长度：" + cd + " 采集时间：" + format.format(time) + "  接收时间："+ format.format(new Date())
                    + " 温度1：" +data.getData1() +"温度2:"+data.getData2()+"湿度1:" + data.getData3() + "湿度2:" +data.getData4() + "经度:" + data.getLng() + "纬度:" + data.getLat()+
                    "状态："+data.getState()+"电压："+data.getDy() +"信号" + data.getXh();
            System.out.println("\n "+"数据上传原始值：" + orignal +"解析成功!结果："+print);
        }
    }

    /**
     * @param content 报文内容
     * @param pos1 入参
     * @param pos2 入参
     * @param pos3 入参
     * @param pos4 入参
     * @param pos5 入参
     * @return 返回值
     */
    private static String getGPSData(String content,int pos1,int pos2,int pos3,int pos4,int pos5){
        DecimalFormat df=new DecimalFormat("0.0000000");
        String tempStr = content.substring(pos4, pos5)+content.substring(pos3, pos4) +content.substring(pos2, pos3) +content.substring(pos1, pos2) ;
        Long integer = Long.valueOf(tempStr, 16);
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(integer)).multiply(new BigDecimal("0.0000001"));
        if(bigDecimal.compareTo(new BigDecimal("180")) == 1){
            bigDecimal = new BigDecimal(String.valueOf(integer)).subtract(new BigDecimal("4294967295")).multiply(new BigDecimal("0.0000001"));
        }
        return df.format(bigDecimal.doubleValue());
    }
}
